package com.model.basemodel.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent

/**
 * @ClassName ActivityCollector
 * @Description 实现activity管理控制(添加、移除、关闭所以、跳转）
 * @Author ：WangZhuang
 * @Date : 2022/5/5 14:09
 * @Version :1.0
 */
object ActivityCollector {
    private val activities = ArrayList<Activity>()
    fun addActivity(activity: Activity) {
        activities.add(activity)
    }

    fun removeActivity(activity: Activity) {
        activities.remove(activity)
    }

    fun finishAll() {
        for (activity in activities) {
            if (!activity.isFinishing) {
                activity.finish()
            }
        }
        activities.clear()
    }

    inline fun <reified T> startActivity(context: Context, block: Intent.() -> Unit) {
        val intent = Intent(context, T::class.java)
        intent.block()
        context.startActivity(intent)
    }
}